package InternetProgram;

import java.io.*;
import java.net.*;
/**
 * @ClassName: ProgramD
 * @Auther: Drawin
 * @Date: 2021/11/22 17:05
 * @Description:
 */

public class ProgramD {
    public static void main(String[] args) {
        try {
            ServerSocket sc = new ServerSocket(3000);
            System.out.println("ProgramD已经启动，正在等待ProgramC的连接...");
            Socket s=sc.accept();
            System.out.println("programC已经连接");
            ProgramReceive pr = new ProgramReceive(s);
            pr.start();
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            DataOutputStream dout = new DataOutputStream(s.getOutputStream());
            while (true){
                String mytmp=br.readLine();
                if(dout!=null)//dout不为空，说明还能发送
                    dout.writeUTF(mytmp);
                else
                    break;
                if (mytmp.trim().equals("bye")){
                    if(dout!=null) dout.close();
                    if (s!=null) s.close();
                    if (br!=null) br.close();
                    break;
                }
            }
        }catch (IOException e){
            System.out.println("发送数据失败："+e.getMessage());
        }
    }
}
